package com.sky.StockHelper;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.sky.StockHelper.entity.MarketCondition;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ParseDomText {

    public static void main(String[] args) {

        String filePath = "C:\\softDevelop\\project\\java\\stockHelper\\src\\test\\java\\com\\sky\\StockHelper\\DomText.txt";
        String html = FileUtil.readString(filePath, "UTF-8");

        //sendGetRequest("https://q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/2/ajax/1/");

        Document document = Jsoup.parse(html);

        Elements rows = document.select("tbody tr"); // 获取所有<tr>元素

        List<MarketCondition> marketConditions = new ArrayList<>();
        for (Element row : rows) {
            Elements tds = row.select("td"); // 获取当前<tr>下的所有<td>元素
            MarketCondition m = new MarketCondition();
            m.setCode(tds.get(1).text());
            m.setName(tds.get(2).text());
            m.setPriceNow(Float.parseFloat(tds.get(3).text()));
            m.setChg(Float.parseFloat(tds.get(4).text()));
            m.setRiseAndFall(Float.parseFloat(tds.get(5).text()));
            m.setRiseSpeed(Float.parseFloat(tds.get(6).text()));
            m.setTurnover(Float.parseFloat(tds.get(7).text()));
            m.setVolumeRatio(Float.parseFloat(tds.get(8).text()));
            m.setAmplitude(Float.parseFloat(tds.get(9).text()));
            m.setTurnoverAmount(tds.get(10).text());
            m.setCirculatingStock(tds.get(11).text());
            m.setCirculatingMarketValue(tds.get(12).text());
            m.setPeRatio(tds.get(13).text());
            m.setTime(new Date());
            m.setCreateTime(new Date());
            marketConditions.add(m);
        }
        System.out.println(marketConditions);
    }

    private static String sendGetRequest(String requestUrl) {
        try {
            HttpRequest request = HttpUtil.createGet(requestUrl);
            request.header("Host", "q.10jqka.com.cn");
            request.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36");
            request.header("Accept-Encoding", "gzip, deflate, br");
            request.header("hexin-v", "A4PSkQA24BAehqzkxxTXEsfMEkwo-BaaUYlbcrVh3N9bfq0yPcinimFc67n1");
            HttpResponse response = request.execute();

            System.out.println(response);
            System.out.println("==");
            System.out.println(response.body());
            System.out.println("===");
            System.out.println(response.body().toString()); // javascrip 搞了一下，拿不到返回的文本，烦
            // <html><body>
            //    <script type="text/javascript" src="//s.thsi.cn/js/chameleon/chameleon.min.1744466.js"></script> <script src="//s.thsi.cn/js/chameleon/chameleon.min.1744466.js" type="text/javascript"></script>
            //    <script language="javascript" type="text/javascript">
            //    window.location.href="//q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/2/ajax/1/";
            //    </script>
            //    </body></html>

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
